// request.js
export default function request(options) {
    return new Promise((resolve, reject) => {
        // console.log("请求参数是：",options)
        uni.request({
            ...options,
            success: (res) => {
                if (res.data.code === 200) {
                    resolve(res.data);
                } else if(res.data.code === 401){
                    uni.showToast({
                        title: '登录失效，请重新登录',
                        icon: 'none',
                        duration: 2000
                    });
                    //清空当前保存的token
                    uni.setStorageSync('userInfo', '');
                    uni.navigateTo({
                        url: '/pages/login/login'
                    });
                }


                else if (res.data.message === '未能读取到有效 token') {
                    uni.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                    //清空当前保存的token
                    uni.setStorageSync('userInfo', '');
                    uni.navigateTo({
                        url: '/pages/login/login'
                    });
                } else if (res.data.message.toString().includes('token 无效')) {
                    uni.showToast({
                        title: '登录失效，请重新登录',
                        icon: 'none',
                        duration: 2000
                    });
                    //清空当前保存的token
                    uni.setStorageSync('userInfo', '');
                    uni.navigateTo({
                        url: '/pages/login/login'
                    });
                }

                else {
                    uni.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
            },
            fail: (err) => {
                // console.log(err)
                reject(err);
            },
            header: {
                Authorization: uni.getStorageSync('tokenValue')
            }
        });
    });
}







